package fashionshop.gui.main.system;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import fashionshop.gui.common.context.UserContext;
import fashionshop.gui.common.event.LoginEvent;
import fashionshop.gui.common.event.LoginListener;
import fashionshop.gui.common.util.MessageBoxUtil;

@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUsername;
	private JTextField txtPassword;
	private LoginListener listener;
	

	private static LoginDialog instance;
	
	public static LoginDialog getInstance(LoginListener listener){
		if(instance == null){
			instance = new LoginDialog();
		}
		instance.listener = listener;
		return instance;
	}

	/**
	 * Create the dialog.
	 */
	private LoginDialog() {
		initComponents();
	}
	private void initComponents() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				txtUsername.setText("");
				txtPassword.setText("");
			}
		});
		setResizable(false);
		setModal(true);
		setTitle("Login");
		setBounds(100, 100, 368, 211);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.EAST);
		
		JLabel lblUsername = new JLabel("T\u00EAn \u0111\u0103ng nh\u1EADp:");
		
		JLabel lblPassword = new JLabel("M\u1EADt kh\u1EA9u:");
		
		txtUsername = new JTextField();
		txtUsername.setColumns(10);
		
		txtPassword = new JPasswordField();
		txtPassword.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblUsername)
						.addComponent(lblPassword))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(txtPassword)
						.addComponent(txtUsername, GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(27)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUsername)
						.addComponent(txtUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(95, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String username = txtUsername.getText();
						String password = txtPassword.getText();
						if(username.isEmpty() || password.isEmpty()){
							MessageBoxUtil.alert(LoginDialog.this, "Nhập tên đăng nhập và mật khẩu");
							return;
						}
						UserContext userContext = UserContext.getInstance();
						if(userContext.employeeLogin(username, password)){
							if(listener != null){
								listener.loginPerformed(new LoginEvent(UserContext.getInstance().getEmployee()));
							}
							LoginDialog.this.setVisible(false);
						}else{
							MessageBoxUtil.error(LoginDialog.this, "Tên đăng nhập hoặc mật khẩu không đúng");
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						LoginDialog.this.setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
